import Vue from 'vue'
import Router from 'vue-router'
import auth from '../utils/auth';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const _import = require('./_import_' + process.env.NODE_ENV)  // 只有开发环境会开启懒加载

Vue.use(Router);

import Layout from '../page/layout/Layout'

let asyncRouterMap=[
  {
    path: '/account',
    component: Layout,
    name:'Account',
    redirect:'/account/company',
    icon: 'user',
    meta:{navName:'账号管理'},
    children:[
      {path:'company', component: _import('account/Company'), name:'Company', meta:{navName:'企业信息'}},
      {path:'car', component: _import('account/Car'), name:'Car', meta:{navName:'车辆信息'}},
      {path:'driver', component: _import('account/Driver'), name:'Driver', meta:{navName:'驾驶员信息'}},
      // {path:'oilcard', component: _import('account/OilCard'), name:'OilCard', meta:{navName:'油卡管理'}},
    ]
  },
  {
    path: '/function',
    component: Layout,
    name:'Function',
    redirect:'/function/limit',
    icon: 'list',
    meta:{navName:'功能管理'},
    children:[
      // {path:'withdraw', component: _import('function/Withdraw'), name:'Withdraw', meta:{navName:'提款申请'}},
      {path:'limit', component: _import('function/Limit'), name:'Limit', meta:{navName:'额度管理'}},
      {path:'order', component: _import('function/Order'), name:'Order', meta:{navName:'订单管理'}},
      {path:'check', component: _import('function/Check'), name:'Check', meta:{navName:'审核管理'}},
    ]
  },
  {
    path: '/message',
    component: Layout,
    name:'Message',
    redirect:'/message/service',
    icon: 'envelope',
    meta:{navName:'消息中心'},
    children:[
      {path:'service', component: _import('message/Service'), name:'Service', meta:{navName:'服务通知'}},
    ]
  }
]

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'Home',
      redirect:'/home',
      component: Layout,
      meta:{navName:'首页'},
      children:[
        {path:'home', component: _import('home/Dashboard'),},
      ]
    },
    {
      path:'/login',
      name:'Login',
      component: _import('home/Login')
    },
    {
      path:'/signin',
      name:'SignIn',
      component: _import('home/SignIn'),
    },
    {
      path:'/forget',
      name:'Forget',
      component: _import('home/Forget'),
    },
    {
      path:'*',
      name:'NotFound',
      component:_import('home/NotFound')
    },
    ...asyncRouterMap,
  ]
})



const noAuthRoutes=[
  'Login',
  'NotFound',
  'SignIn',
  'Forget',
]

router.beforeEach((to, from, next) => {
  NProgress.start()
  let loggedIn = auth.getToken();
  // // 已登陆
  // if(to.name==='Login' && getToken){
  //   next({
  //     path:'/',
  //   })
  //   return;
  // }
  // // 未登陆
  // else
    if(noAuthRoutes.indexOf(to.name)<0 && !loggedIn){
    next({
      path:'/login',
      query:{
        redirect: to.fullPath
      }
    })
    return;
  }
  next();
})

router.afterEach(() => {
  NProgress.done()
})

export {asyncRouterMap};
export default router;
